﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DDHDeepTunnel.DeepScheduling.Data;
using DDHDeepTunnel.DeepScheduling.Interfaces;

namespace DDHDeepTunnel.DeepScheduling.Business
{
    internal class SystemLogInfo : ISystemLogInfo
    {
        private SystemLogQueryDAO _queryDao;
        private SystemLogWritingDAO _writeDao;

        public SystemLogInfo(SystemLogQueryDAO querydao, SystemLogWritingDAO writingDao, OnlineDataModule module)
        {
            this._queryDao = querydao;
            this._writeDao = writingDao;
        }

        public IEnumerable<SystemLog> GetAllSystemLogInfo()
        {
            return this._queryDao.GetAllSystemLogInfo();
        }

        public IEnumerable<SystemLog> GetSystemLogInfoByLogType(string logType)
        {
            return this._queryDao.GetSystemLogInfoByLogType(logType);
        }

        public IEnumerable<SystemLog> GetSystemLogInfoByLogType(string logType, DateTime startTime,
            DateTime endTime)
        {
            return this._queryDao.GetSystemLogInfoByLogType(logType, startTime,
                endTime);
        }

        public IEnumerable<SystemLog> GetSystemLogInfoByLogTypes(string logTypes, DateTime startTime,
            DateTime endTime)
        {
            return this._queryDao.GetSystemLogInfoByLogTypes(logTypes, startTime,
                endTime);
        }

        public bool AddSystemLogInfo(SystemLog systemLog)
        {
            try
            {
                var logType = LogType.UnKnown;
                try
                {
                    logType = (LogType)Enum.Parse(typeof(LogType), (string)systemLog.LogType, true);
                }
                catch (Exception ex)
                {
                    systemLog.LogContent = ex.Message;
                }
                var systemLogInfo = new SystemLog();
                systemLogInfo.LogType = logType.ToString();
                systemLogInfo.LogName = systemLog.LogName;
                systemLogInfo.LogTime = systemLog.LogTime;
                systemLogInfo.LogFrom = systemLog.LogFrom;
                systemLogInfo.UserName = systemLog.UserName;
                systemLogInfo.Ip = systemLog.Ip;
                systemLogInfo.SessionId = systemLog.SessionId;
                systemLogInfo.ResultType = systemLog.ResultType;
                systemLogInfo.LogContent = systemLog.LogContent;
                systemLogInfo.Remark = systemLog.Remark;
                systemLogInfo.InsertTime = System.DateTime.Now;
                systemLogInfo.StateInfo = systemLog.StateInfo;
                systemLogInfo.Isuse = 1;
                systemLogInfo.Version = Guid.NewGuid().ToString();
                this._writeDao.Add(systemLogInfo);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
    }
}
